package com.gqzm.iaas.interceptor;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: {@link HttpLogAopInterceptor}
 * @description: http请求日志拦截处理
 * @author: peter qin
 * @date: 2023 07 24
 * @version: 1.0
 **/
@Slf4j
@Aspect
@Component
public class HttpLogAopInterceptor {


    @Pointcut(value = "execution(public * com.gqzm.*.controller.*.*(..)) " +
            "&& !@annotation(com.gqzm.iaas.annotation.LogIgnore)")
    public void httpLogPointCut() {
        //http请求控制层切点
    }


    @SneakyThrows
    @Around("httpLogPointCut()")
    public Object around(ProceedingJoinPoint point) {
        //获取request信息
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        long start = System.currentTimeMillis();
        log.info("请求参数 [{}]: {}", request.getRequestURI(), JSON.toJSONString(point.getArgs()[0]));
        Object result = point.proceed(point.getArgs());
        log.info("返回参数 [{}]: {}", request.getRequestURI(), JSON.toJSONString(result));
        long end = System.currentTimeMillis();
        log.info("请求耗时 [{}]: {} 毫秒", request.getRequestURI(), end - start);
        return result;
    }



}
